package org.hwadee.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.hwadee.entity.Category;
import org.hwadee.entity.Comment;
import org.hwadee.entity.Post;
import org.hwadee.mapper.CommentMapper;
import org.hwadee.mapper.PostMapper;
import org.hwadee.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentService {

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    PostMapper postMapper;

    @Autowired
    UserMapper userMapper;


    public List<Comment> listCommentByPostId(Integer postId) {
        List<Comment> comments = commentMapper.listCommentByPostId(postId);
        for(Comment comment : comments) {
            comment.setCommentAuthorName(userMapper.getUserById(comment.getCommentUserId()).getNickname());
        }
        return comments;
    }

    /**
     * 获得最近评论
     *
     * @param limit 查询数量
     * @return 评论列表
     */
    public List<Comment> listRecentComment(int limit) {
        List<Comment> commentList = null;
        try {
            commentList = commentMapper.listRecentComment(limit);
            for (int i = 0; i < commentList.size(); i++) {
                Post post = postMapper.getPostByIdAndStatus(commentList.get(i).getCommentPostId(), 1);
                commentList.get(i).setPost(post);
                commentList.get(i).setCommentAuthorName(userMapper.getNicknameByUserId(commentList.get(i).getCommentUserId()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return commentList;
    }

    /**
     * 获得子评论
     * @param id
     * @return
     */
    public List<Comment> listChildComment(Integer id) {
        List<Comment> childCommentList = null;
        try {
            childCommentList = commentMapper.listChildComment(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return childCommentList;
    }

    public List<Comment> listRecentCommentById(Integer userId, int i) {
        List<Comment> commentList = null;
        try {
            commentList = commentMapper.listRecentCommentById(userId, i);
            for (int j = 0; j < commentList.size(); i++) {
                Post post = postMapper.getPostByIdAndStatus(commentList.get(j).getCommentPostId(), 1);
                commentList.get(j).setPost(post);
                commentList.get(j).setCommentAuthorName(userMapper.getNicknameByUserId(commentList.get(j).getCommentUserId()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return commentList;
    }

    public void insertComment(Comment comment) {
        commentMapper.insertComment(comment);
    }

    public List<Comment> getCommentsByCommentUserId(Integer userId) {
        return commentMapper.getCommentsByCommentUserId(userId);
    }

    public List<Comment> getAllComments() {
        return commentMapper.getAllComments();
    }

    public List<Comment> getReceiveCommentByUserId(Integer userId) {
        List<Integer> commentId = getCommentIdByUserId(userId);
        return commentMapper.getReceiveCommentByUserId(userId, commentId);
    }



    public List<Integer> getCommentIdByUserId(Integer userId) {
        return commentMapper.getCommentIdByUserId(userId);
    }

    public Comment getCommentByCommentId(Integer id) {
        return commentMapper.getCommentByCommentId(id);
    }

    public void deleteCommentByCommentId(Integer id) {
        commentMapper.deleteCommentByCommentId(id);
    }

    public void updateComment(Comment comment) {
        commentMapper.updateComment(comment);
    }

    public List<Comment> getCommentByPostId(Integer commentPostId) {
        return commentMapper.getCommentByPostId(commentPostId);
    }

    public PageInfo<Comment> pageComment(Integer pageIndex, Integer pageSize, Comment comment) {
        PageHelper.startPage(pageIndex, pageSize);
        Post post = postMapper.getPostByPostId(comment.getCommentPostId());
        List<Comment> commentList = commentMapper.getCommentByPostId(comment.getCommentPostId());
        for (int i = 0; i < commentList.size(); i++) {
            commentList.get(i).setPost(post);
            commentList.get(i).setParentComment(commentMapper.getCommentByCommentId(commentList.get(i).getCommentPid()));
            commentList.get(i).setCommentAuthorName(userMapper.getNicknameByUserId(commentList.get(i).getCommentUserId()));
        }

        return new PageInfo<>(commentList);
    }

    public void deleteCommentByPostId(Integer id) {
        commentMapper.deleteCommentByCommentPostId(id);
    }

    public void recoveryCommentByPostId(Integer id) {
        commentMapper.recoveryCommentByCommentPostId(id);
    }

    public Integer getPostIdByCommentId(Integer id) {
        return commentMapper.getPostIdByCommentId(id);
    }
}
